package abv.jonnyhonda.com;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;

public class ABVActivity extends Activity implements OnSeekBarChangeListener, OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      	
        /*
         * Set the slider to their initial positions, populate the text boxes and perform the first calculation	
         */
		
			TextView textView4 = (TextView) findViewById(R.id.textView4);				
			textView4.setText(Integer.toString(0 + 998)); // here in textView the percent will be shown
			TextView textView1 = (TextView) findViewById(R.id.textView1);				
			textView1.setText(Integer.toString(202 + 998)); // here in textView the percent will be shown
			calcABV();
			try{
				TextView textView8 = (TextView) findViewById(R.id.textView8);		
				textView8.setText(Integer.toString(calcBrix(0 + 998)));
				TextView textView9 = (TextView) findViewById(R.id.textView9);		
				textView9.setText(Integer.toString(calcBrix(202 + 998)));
			}catch(Exception e){
				
			}

        /*
         * set up objects and listeners
         */
		SeekBar seekStartSG = (SeekBar)findViewById(R.id.seekBar1);      
        SeekBar seekEndSG = (SeekBar)findViewById(R.id.seekBar2);
        Button button1 = (Button)findViewById(R.id.button1);
        
        /*
         * OnClick listener for first minus button
         */
        try{
        button1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				TextView textView1 = (TextView) findViewById(R.id.textView1);
				Integer strVal = Integer.parseInt(textView1.getText().toString());
				strVal --;
				textView1.setText(Integer.toString(strVal)); // here in textView the percent will be shown
				SeekBar seekStartSG = (SeekBar)findViewById(R.id.seekBar1);
				//msgBox(strVal.toString());
            	seekStartSG.setProgress(strVal-998);
				calcABV();
			}
       });

  
        Button button2 = (Button)findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
				TextView textView4 = (TextView) findViewById(R.id.textView4);
				Integer strVal = Integer.parseInt(textView4.getText().toString());
				strVal --;
				textView4.setText(Integer.toString(strVal)); // here in textView the percent will be shown
				SeekBar seekbar = (SeekBar)findViewById(R.id.seekBar2);
				//msgBox(strVal.toString());
            	seekbar.setProgress(strVal-998);
				calcABV();
            }
        });
        
        
        Button button3 = (Button)findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
				TextView textView1 = (TextView) findViewById(R.id.textView1);
				Integer strVal = Integer.parseInt(textView1.getText().toString());
				strVal ++;
				textView1.setText(Integer.toString(strVal)); // here in textView the percent will be shown
				SeekBar seekStartSG = (SeekBar)findViewById(R.id.seekBar1);
				//msgBox(strVal.toString());
            	seekStartSG.setProgress(strVal-998);
				calcABV();	
            }
        });
        
        
        Button button4 = (Button)findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
				TextView textView4 = (TextView) findViewById(R.id.textView4);
				Integer strVal = Integer.parseInt(textView4.getText().toString());
				strVal ++;
				textView4.setText(Integer.toString(strVal)); // here in textView the percent will be shown
				SeekBar seekbar = (SeekBar)findViewById(R.id.seekBar2);
				//msgBox(strVal.toString());
            	seekbar.setProgress(strVal-998);
				calcABV();
            }
        });
        }catch(Exception e){
        	
        }
   
    	// Implement the setOnSeekBarChangeListener callback for seekStartSG
        seekStartSG.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
		public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
			// TODO Auto-generated method stub
			try{
				TextView textView1 = (TextView) findViewById(R.id.textView1);				
				textView1.setText(Integer.toString(arg1 + 998)); // here in textView the percent will be shown
				
				calcABV();
			}catch (Exception e){
				
			}
		}



		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub			
		}

		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub			
		}});
  
    	// Implement the setOnSeekBarChangeListener callback for seekEndSG
        seekEndSG.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
		public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
			// TODO Auto-generated method stub
			try{
				TextView textView4 = (TextView) findViewById(R.id.textView4);				
				textView4.setText(Integer.toString(arg1 + 998)); // here in textView the percent will be shown
				calcABV();
			}catch (Exception e){
				
			}
		}

		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}

		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
		}});        
        
        
    }

    /**
     * 
     * @param sg
     * @return
     * 
     * =261.3 *(1 - 1/SG)
     */
    public int calcBrix(int sg){
    	double dSg = sg / 1000;
		return (int) (261.3 * (1.0 - 1.0/dSg));   	
    }
    
	public void calcABV() {
		// (((OG-FG) * 1.05) / FG) / .79
		TextView textView1 = (TextView) findViewById(R.id.textView1);	
		double OG = Double.parseDouble(textView1.getText().toString());
		
		TextView textView4 = (TextView) findViewById(R.id.textView4);	
		double FG = Double.parseDouble(textView4.getText().toString());
		
		try{
			double abv = ((((OG-FG) * 1.05) / FG) / .79) * 100;
			TextView textView5 = (TextView) findViewById(R.id.textView5);
			textView5.setText("" + String.format("%.2f", abv) + "%");
		}catch (Exception e){
			e.getLocalizedMessage();
		}
		try{
			TextView textView8 = (TextView) findViewById(R.id.textView8);	
			int B9 = calcBrix(OG);
			textView8.setText(Integer.toString(B9));
			int A9 = calcBrix(FG);
			TextView textView9 = (TextView) findViewById(R.id.textView9);		
			textView9.setText(Integer.toString(A9));
			TextView textView11 = (TextView) findViewById(R.id.textView11);	
			//1.0 * (FG) * (A9 -B9) / (100 -A9)
			int sugarPerlitre = (int) (1 * (FG) * (A9 -B9) / (100 -A9));
			textView11.setText(Integer.toString(sugarPerlitre));
			
			
		}catch(Exception e){
			
		}
		
	}
    
	private int calcBrix(double oG) {
		// TODO Auto-generated method stub
    	double dSg = oG / 1000;
		return (int) (261.3 * (1.0 - 1.0/dSg));
	}

	public void msgBox(String text){
		Context context = getApplicationContext();
		//CharSequence text = "Hello toast!";
		int duration = Toast.LENGTH_SHORT;

		Toast toast = Toast.makeText(context, text, duration);
		toast.show();
	}
	
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub
		
	}

	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}

	public void onStopTrackingTouch(SeekBar seekBar) {
		
	}

	public void onClick(View paramView) {
		// TODO Auto-generated method stub
		
	}

}
